<?php

namespace App\Http\Controllers\Api\User;

use App\Http\Controllers\ApiController;

use Illuminate\Http\Request;
use App\Http\Models\User\UserBaseModel;
use Illuminate\Support\Facades\Redis;



class UCenterController extends ApiController
{
    public function __construct()
    {
        parent::__construct();
    }


    /**
     * @return $this
     * @throws \ErrorException
     * 获取意见反馈分类列表
     */
    public function getFeedBackCate()
    {
        $data = UserBaseModel::getFeedBackCate();
        return self::response($data,200);
    }

    /**
     * @param Request $request
     * @return $this
     * @throws \ErrorException
     * 提交意见反馈
     */
    public function submitFeedBack(Request $request)
    {
        $data = $request->all();
        if (empty($data['sug_cid'])){
            return self::response(null,'-1','请选择分类');
        }

        if (empty($data['uid'])){
            return self::response(null,'-2','用户id不能为空');
        }


        if ($data['sug_cid']!=6){
            $tmp['sug_cid'] = $data['sug_cid'];
            $tmp['uid']     = $data['uid'];
            $flag =  UserBaseModel::addSuggestion($tmp);
            if ($flag!==false){
                return self::response(null,'200','操作成功');
            }else
                return self::response(null,-5,'操作失败');
        }

        if (!empty($data['address'])){
            $tmp['address'] = $data['address'];
        }

        if (empty($data['content'])){
            return self::response(null,-4,'请填写联系反馈内容');
        }

        $tmp['sug_cid'] = $data['sug_cid'];
        $tmp['uid']     = $data['uid'];
        $tmp['content'] = $data['content'];


       $flag =  UserBaseModel::addSuggestion($tmp);

        if ($flag!==false){
           return self::response(null,'200','操作成功');
        }else
            return self::response(null,-5,'操作失败');
    }


    /**
     * @param Request $request
     * @return $this
     * @throws \ErrorException
     * 我的用户中心
     */
    public function myUCenter(Request $request)
    {
        $data = $request->all();

        if (empty($data['uid']))
        {
            return self::response(null,'-1','用户不能为空');
        }
        $myUserInfo = UserBaseModel::getUserInfo($data['uid']);

        if (empty($myUserInfo)){
            return self::response(null,-2,'用户信息不存在');
        }
        return self::response($myUserInfo,200);
    }



    public function follow(Request $request)
    {
        $data = $request->all();
        if (empty($data['uid'])||empty($data['to_uid'])){
            return self::response('','-1','数据异常');
        }
        $res = UserBaseModel::addFollow($data['uid'],$data['to_uid']);
        if ($res['status']==false){
            return self::response(null,'-1','网络错误，操作失败');
        }elseif($res['status'] == 3&&isset($res['msg'])){
            return self::response(null,'-2',$res['msg']);
        }else{
            return self::response(null,200,'关注成功');
        }
    }


    /**
     * @param Request $request
     * @return $this
     * @throws \ErrorException
     */
    public function addAddress(Request $request)
    {
        $input = $request->all();
        if (empty($input['uid'])){
            return self::response(null,'-1','用户id为空');
        }

        if (empty($input['phone'])){
            return self::response(null,'-1','请填写手机号');
        }

        if (empty($input['name'])){
            return self::response(null,'-1','请填写姓名');
        }
        if (empty($input['province'])){
            return self::response(null,'-1','请选择省');
        }

        if (empty($input['city'])){
            return self::response(null,'-1','请选择市');
        }

        if (empty($input['dist'])){
            return self::response(null,'-1','请选择区');
        }

        if (empty($input['content'])){
            return self::response(null,'-1','请填写详细地址');
        }
        $res= UserBaseModel::addAddress($input);
        return self::response('',$res['ret'],$res['msg']);
    }


    /**
     * @param Request $request
     * @return $this
     * @throws \ErrorException
     */
    public function editAddress(Request $request)
    {
        $input = $request->all();
        if (empty($input['address_id'])){
            return self::response(null,'-1','地址id为空');
        }

        $res = UserBaseModel::editAddress($input);
        return self::response('',$res['ret'],$res['msg']);
    }


    /**
     * @param Request $request
     * @return $this
     * @throws \ErrorException
     */
    public function addressList(Request $request)
    {
        $input = $request->all();
        if (empty($input['uid'])){
            return self::response(null,'-1','用户id为空');
        }
        $data = UserBaseModel::getAddressById($input['uid']);

        if (empty($data)){
            return self::response(null,200);
        }else{
            $user = UserBaseModel::getUser($input['uid']);
            foreach ($data as &$val){
                $val = get_object_vars($val);
                if ($val['id'] == $user['defaultAddress']){
                    $val['is_default'] = 1;
                }else{
                    $val['is_default'] = 0;
                }
            }
            if (empty($user['defaultAddress'])){
                $data[0]['is_default'] = 1;
            }
            return self::response($data,200);
        }
    }


    /**
     * @param Request $request
     * @return $this
     * @throws \ErrorException
     */
    public function delAddress(Request $request)
    {
        $input = $request->all();
        if (empty($input['address_id'])){
            return self::response(null,'-1','地址id不能为空');
        }

        if (empty($input['uid'])){
            return self::response(null,'-1','uid不能为空');
        }

        $input['address_id'] = json_decode($input['address_id']);

        if (!is_array($input['address_id'])||empty($input['address_id'])){
            return self::response(null,'-1','地址id必须为数组');
        }
        $res = UserBaseModel::delAddress($input['address_id'],$input['uid']);
        if ($res>0){
            return self::response(null,200,'删除成功');
        }else{
            return self::response(null,-2,'删除失败');
        }
    }


    /**
     * @param Request $request
     * @return $this
     * @throws \ErrorException
     */
    public function setDefaultAddress(Request $request)
    {
        $input = $request->all();
        if (empty($input['address_id'])){
            return self::response(null,'-1','地址id不能为空');
        }
        if (empty($input['uid'])){
            return self::response(null,'-2','uid不能为空');
        }
        if (!is_numeric($input['address_id'])){
            return self::response(null,'-1','地址id错误');
        }
        $res = UserBaseModel::setDefaultAddress($input['uid'],$input['address_id']);
        if ($res>0){
            return self::response(null,'200','设置成功');
        }else{
            return self::response(null,-3,'设置失败');
        }
    }





    public function version()
    {
        $data = UserBaseModel::getVersion();
        if (empty($data)){
            return self::response(null,-1,'暂无信息');
        }else{
            return self::response($data);
        }
    }


    public function getBalanceRecord(Request $request)
    {
        $input = $request->all();
        if (empty($input['uid'])){
            return self::response(null,-1,'uid is null');
        }

        if (empty($input['amount_type'])){
            return self::response(null,-2,'amount_type is null');
        }

        if (!isset($input['nowPage'])){
            return self::response(null,-3,'当前页不能为空');
        }


        $skip = $input['nowPage']  *10;

        $data = UserBalanceModel::getBalanceRecord($input['uid'],$input['amount_type'],$skip);
        if (!empty($data)){
            foreach ($data as &$val){
                $val['create_at'] = date('Y-m-d H:i:s',$val['create_at']);
            }
        }
        return self::response($data,200);
    }


    public function bindInviteCode(Request $request)
    {
        $input = $request->all();
        if (empty($input['uid'])){
            return self::response(null,-1,'uid is null');
        }

        if (empty($input['Invite_code'])){
            return self::response(null,-2,'Invite_code is null');
        }

        #根据邀请码 获取 邀请码用户 信息
        $Invite   = UserBaseModel::getUserByInviteCode($input['Invite_code']);

        if (empty($Invite)||empty($Invite->id)||($Invite->id == $input['uid'])){
            return self::response(null,-3,'邀请码错误');
        }
        $flag     = UserBaseModel::bindInviteCode($input['uid'],$Invite->id);
        if ($flag != false){
            return self::response(null,200);
        }else{
            return self::response(null,200,'绑定失败，稍后再试');
        }
    }


    public function ApplyForMailingList(Request $request)
    {
        $input = $request->all();
        if (empty($input['uid'])){
            return self::response(null,-1,'uid is null');
        }

        if (empty($input['apply_ids'])){
            return self::response(null,-2,'apply_ids is null');
        }

        $apply_ids = json_decode($input['apply_ids']);

        if (!is_array($apply_ids)){
            return self::response(null,-3,'doll_id error');
        }

        $data['list']           = UserDollsModel::getDollInfo($input['uid'],$apply_ids);
        $count                  = count($data['list']);
        $postage                = 0;
        $payment_status         = 0;

        if (empty($data['list'])){
            return self::response(null,'-4','数据错误');
        }

        if ($count<2&&$data[0]['num']<2){
            $postage            = 10;
            $payment_status     = 1;
        }

       $order['postage']               = $postage;
       $order['payment_status']        = $payment_status;
       $order['apply_ids']             = $apply_ids;
       $data['order']                  = $order;

       return self::response($data);

    }


    public function ApplyForMailing(Request $request)
    {
        $input = $request->all();
        if (empty($input['order'])){
            return self::response(null,-1,'order is null');
        }

        if (empty($input['uid'])){
            return self::response(null,-2,'uid is null');
        }

        $order = json_decode($input['order'],true);
        if (empty($order['apply_ids'])||!is_array($order['apply_ids'])){
            return self::response(null,-3,'apply_ids is null or not array');
        }

        if (!isset($order['postage'])){
            return self::response(null,-4,'postage is null');
        }

        if (!isset($order['payment_status'])){
            return self::response(null,-5,'payment_status is null');
        }

        if (empty($input['address_id'])){
            return self::response(null,-6,'address_id is null');
        }

        $data   = UserDollsModel::getDollData($input['uid'],$order['apply_ids']);
        $sn                                 = Controller::get_order_sn();
        $sn                                .= '_V2';

        #获取地址信息
        $address = UserBaseModel::getAddressByAddressId($input['address_id']);

        if (empty($address)){
            return self::response(null,-7,'地址为空');
        }

        $address = get_object_vars($address);

        //        #写入记录
        $flag    = ApplyMailModel::insertMailRecord($data,$order['apply_ids'],$order['payment_status'],$order['postage'],$address,$sn,$input['uid']);

        if ($flag>0){
            if ($order['payment_status']==1){
                return self::response(['order_sn'=>$sn],200,'少于2件，需要支付邮费10元');
            }else{
                return self::response(['order_sn'=>$sn],200);
            }
        }else{
            return self::response(null,-7,'操作失败，稍后再试');
        }
    }

    /**
     * 付邮费
     */
    public function editMailingStat(Request $request)
    {
        if (!$request->input('order_sn'))
        {
            return self::response([],40001,'缺少参数order_sn');
        }
        $apply = ApplyMailingModel::where('order_sn','=',$request->input('order_sn'))->select()->first();
        if (empty($apply))
        {
            return self::response([],40002,'未查询到订单');
        }
        if ($apply->payment_status != 1)
        {
            return self::response([],40003,'订单状态不需要支付');
        }

        $pay     = new AliPayController();
        return $pay->pay($request->input('order_sn'), $apply->postage,"娃娃提货付邮费-{$apply->postage}CNY");

    }


    /**
     * 根据id改密码
     * @param Request $request
     * @return mixed
     * @throws \ErrorException
     */
    public function modifyPwdById(Request $request)
    {
        $input      = $request->all();
        if (empty($input['uid'])){
            return self::response(null,'-1','empty uid');
        }

        if (empty($input['user_password'])){
            return self::response(null,'-1','empty user_password');
        }

       $data =  UserBaseModel::updateUser($input['uid'],['user_password'=>$input['user_password']]);
        return self::response($data['data'],$data['ret'],$data['msg']);
    }















}
